package ru.hh.team02.chat.client;

import ru.hh.team02.chat.server.Services;
// XXX [Oleg] The biggest problem here is that we don't process the situations where
// the recipient may not exist or be mistyped. I think we should respond to such messages
// to the sender and log appropriately.
// XXX [Yan] now sending service responsible for that
/**
 * Class with methods to handle messages.
 */
public class MessageHandler {
    private String clientName;

    /**
     *
     * @param clientName Client name the messages are addressed to.
     */
    public MessageHandler(String clientName) {
        this.clientName = clientName;
    }

    /**
     * Mark message in the DB as delivered or delete (depends on implementation)
     * for it to not longer be sent to the user on their login.
     * @param messageId Unparsed message id.
     */
    public void markMessageInDb(String messageId) {
        // XXX [Oleg] Shouldn't we catch parsing errors and log them?
        int Id = Integer.parseInt(messageId);
        Services.dbService.markPersonalMessageAsDelivered(Id);
    }

    /**
     * Handle message to change room.
     * @param message Unparsed message.
     */
    public void changeRoom(String message) {
        String[] info = message.split(" ");
        if (info.length == 2) {
            String roomName = info[1];

            if (Services.REGISTRY.changeRoom(clientName, roomName)) {
                Services.dbService.changeRoom(clientName, roomName);

                Services.MESSENGER.sendServiceMessage(String.format("Welcome to %s!", roomName), clientName);
                Services.ADMIN_LOGGER.write("Client \"" + clientName
                        + "\" entered in new room \"" + roomName + "\"");
            } else {
                Services.MESSENGER.sendServiceMessage(String.format(
                        "server> no such room\n" +
                                "\"Possible rooms are: %s.", Services.REGISTRY.getRoomNames().toString()),
                        clientName);
                Services.ADMIN_LOGGER.write("Client \"" + clientName
                        + "\" tried to switch to nonexistent room");
            }
        }
    }

    /**
     * Send personal message to the client specified inside the message.
     * @param message Unparsed private message.
     */
    // XXX [Oleg] What if we fail to send it? E.g. recipient doesn't exist, etc.
    // XXX [Yan] this responsibilities on sending service
    public void sendPersonalMessage(String message) {
        String[] info = message.split(" ", 3);
        if (info.length == 3) {
            String recepientName = info[1];
            String messageText = info[2];


            Services.MESSENGER.SendPersonalMessage(
                    "personal:" + clientName + "> " + messageText,
                    recepientName,
                    clientName);
        }
    }

    /**
     * Send the message to the room the client this handler belongs to is in.
     * @param message Message to send.
     */
    public void sendMessageToRoom(String message) {
        Services.MESSENGER.sendMessageToRoom(clientName + "> " + message,
                clientName);
        Services.ADMIN_LOGGER.write("Client \"" + clientName
                + "\" sent new message: \"" + message + "\"");
    }

}
